package com.foreknow.service.impl;

import com.foreknow.dao.AdminDao;
import com.foreknow.dao.inpl.AdminDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;

import java.sql.Connection;
import java.sql.SQLException;

public class AdminServiceImpl implements AdminService {
    @Override
    public Admin isValidate(String username, String password) {
        //1. 链接数据库
        DBManager dbManager= DBManager.getInstance();
        Connection conn =dbManager.getConnection();

        AdminDaoImpl adminDao = new AdminDaoImpl();
        //调用BaseDao中的Connection(conn)方法
        adminDao.setConnection(conn);
        Admin admin= adminDao.isLogin(username,password);
        return admin;
    }

    @Override
    public boolean save(Admin admin) {
        //1. 链接数据库
        DBManager dbManager= DBManager.getInstance();
        Connection conn =dbManager.getConnection();

        AdminDaoImpl adminDao = new AdminDaoImpl();
        //调用BaseDao中的Connection(conn)方法
        adminDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = adminDao.addAdmin(admin);
            //手动提交事务提交事务
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务取消之前的操作
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }


}
